package com.cskaoyan.converter;

import lombok.SneakyThrows;
import org.springframework.core.convert.converter.Converter;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;

@Component
public class StringArrays2DateArraysConverter implements Converter<String[], Date[]> {

    @SneakyThrows
    @Override
    public Date[] convert(String[] source) {
        int i = 0;
        Date[] dates = new Date[source.length];
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        for (String s : source) {
            Date parse = simpleDateFormat.parse(s);
            dates[i++] = parse;
        }
        return dates;
    }
}
